package com.gitee.zycra.future.job.manager;

import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.model.JobMetaData;
import com.gitee.zycra.future.job.common.param.PageQueryMetaDataParam;
import com.gitee.zycra.future.job.common.util.IdGenerator;
import com.gitee.zycra.future.job.common.util.UserInfoUtil;
import com.gitee.zycra.future.job.common.vo.JobMetaDataVO;
import com.gitee.zycra.future.job.dao.JobMetaDataDao;
import com.gitee.zycra.jdbc.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zycra
 */
@Component
public class JobMetaDataManager {

    @Autowired
    private JobMetaDataDao jobMetaDataDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 插入
     *
     * @param jobMetaData 元数据对象
     * @return 插入条数
     */
    public int insertJobMetaData(JobMetaData jobMetaData) {
        jobMetaData.setId(idGenerator.nextId()).setOperatorId(UserInfoUtil.getUserId()).setOperatorName(UserInfoUtil.getUsername());
        return jobMetaDataDao.insertJobMetaData(jobMetaData);
    }

    /**
     * 删除
     *
     * @param id 要删除的ID
     * @return 删除条数
     */
    public int deleteJobMetaData(Long id) {
        return jobMetaDataDao.updateJobMetaData(new JobMetaData()
                .setId(id).setDeleted(true)
                .setOperatorId(UserInfoUtil.getUserId())
                .setOperatorName(UserInfoUtil.getUsername()));
    }

    /**
     * 更新
     *
     * @param jobMetaData 元数据对象
     * @return 更新条数
     */
    public int updateJobMetaData(JobMetaData jobMetaData) {
        jobMetaData.setOperatorId(UserInfoUtil.getUserId()).setOperatorName(UserInfoUtil.getUsername());
        return jobMetaDataDao.updateJobMetaData(jobMetaData);
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    public PageResult<JobMetaDataVO> selectJobMetaDataForPage(PageQuery<PageQueryMetaDataParam> pageQuery) {
        return jobMetaDataDao.selectJobMetaDataForPage(pageQuery);
    }

    /**
     * 根据元数据配置ID查询
     *
     * @param jobMetaDataId 元数据配置ID
     * @return 元数据
     */
    public JobMetaData selectById(Long jobMetaDataId) {
        return jobMetaDataDao.selectById(jobMetaDataId);
    }
}
